home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Extra 1996 #3 / AmigaPlus_CD-ROM-EXTRA_Nr.3.bin / aminet-spiele / role on / larn / movem.c < prev    next >
C/C++ Source or Header  |  1997-12-31  |  19KB  |  581 lines

  1. /*
  2.  *  movem.c (move monster)
  3.  *
  4.  *  movemonst()     Routine to move the monsters toward the player
  5.  *  build_proximity_ripple()  Build proximity ripple for smart monster move
  6.  *  move_scared()   Move scared monsters
  7.  *  move_smart()    Move smart monsters
  8.  *  move_dumb()     Move dumb monsters
  9.  *  mmove(x,y,xd,yd)    Function to actually perform the monster movement
  10.  */
  11. #include "header.h"
  12. #include "larndefs.h"
  13. #include "monsters.h"
  14. #include "objects.h"
  15. #include "player.h"
  16.  
  17. #define min(x,y) (((x)>(y))?(y):(x))
  18. #define max(x,y) (((x)>(y))?(x):(y))
  19.  
  20.        void movemonst();
  21. static void build_proximity_ripple();
  22. static void move_scared();
  23. static void move_smart();
  24. static void move_dumb();
  25. static void mmove();
  26.  
  27. #if 0
  28. # define IDISTNORM   8  /* was 17 - dgk */
  29. # define IDISTAGGR  20  /* was 40 - dgk */
  30. #endif
  31. # define IDISTNORM  17  /* was 17 - dgk */
  32. # define IDISTAGGR  40  /* was 40 - dgk */
  33.  
  34. static short w1x[9],w1y[9];
  35. static int tmp1,tmp2,tmp3,tmp4,distance;
  36.  
  37. /* list of monsters to move */
  38. static struct foo { char x ; char y; char smart; } movelist[250] ;
  39.  
  40. /*
  41.  *  movemonst()     Routine to move the monsters toward the player
  42.  *
  43.  *  This routine has the responsibility to determine which monsters are to
  44.  *  move, and call movemt() to do the move.
  45.  *  Returns no value.
  46.  */
  47. void movemonst()
  48.     {
  49.     register int i,j,movecnt=0, smart_count, min_int ;
  50.     if (c[HOLDMONST])  return;  /* no action if monsters are held */
  51.  
  52.     if (c[AGGRAVATE])   /* determine window of monsters to move */
  53.       {
  54.       tmp1=playery-5; tmp2=playery+6; tmp3=playerx-10; tmp4=playerx+11;
  55.       distance=IDISTAGGR; /* depth of intelligent monster movement */
  56.       }
  57.     else
  58.       {
  59.       tmp1=playery-3; tmp2=playery+4; tmp3=playerx-5; tmp4=playerx+6;
  60.       distance=IDISTNORM; /* depth of intelligent monster movement */
  61.       }
  62.  
  63.     if (level == 0) /* if on outside level monsters can move in perimeter */
  64.         {
  65.         if (tmp1 < 0) tmp1=0;        if (tmp2 > MAXY) tmp2=MAXY;
  66.         if (tmp3 < 0) tmp3=0;        if (tmp4 > MAXX) tmp4=MAXX;
  67.         }
  68.     else /* if in a dungeon monsters can't be on the perimeter (wall there) */
  69.         {
  70.         if (tmp1 < 1) tmp1=1;        if (tmp2 > MAXY-1) tmp2=MAXY-1;
  71.         if (tmp3 < 1) tmp3=1;        if (tmp4 > MAXX-1) tmp4=MAXX-1;
  72.         }
  73.  
  74.     /* We now have a window in which to move monsters.  First find all
  75.        monsters in the window, then decide whether or not to move them.
  76.        Its faster that way since the size of the window is usually larger
  77.        than the # of monsters in that window.
  78.  
  79.        Find all monsters in the window.  The only time a monster cannot
  80.        move is if: monsters are not aggrevated, AND player is stealthed,
  81.        AND the monster is asleep due to stealth.  Split into two
  82.        separate loops in order to simplify the if statement inside the
  83.        loop for the most common case.
  84.  
  85.        Also count # of smart monsters.
  86.     */
  87.     smart_count = 0 ;
  88.     min_int = 10 - c[HARDGAME] ;    /* minimum monster intelligence to move smart */
  89.     if ( c[AGGRAVATE] || !c[STEALTH] )
  90.         {
  91.         for ( j = tmp1 ; j < tmp2 ; j++ )
  92.             for ( i = tmp3 ; i < tmp4 ; i++ )
  93.                 if (mitem[i][j])
  94.                     {
  95.                     movelist[movecnt].x = i;
  96.                     movelist[movecnt].y = j ;
  97.                     if ( monster[mitem[i][j]].intelligence > min_int )
  98.                         {
  99.                         movelist[movecnt].smart = TRUE ;
  100.                         smart_count++;
  101.                         }
  102.                     else
  103.                         movelist[movecnt].smart = FALSE ;
  104.                     movecnt++;
  105.                     }
  106.         }
  107.     else
  108.         {
  109.         for ( j = tmp1; j < tmp2 ; j++ )
  110.             for ( i = tmp3 ; i < tmp4 ; i++ )
  111.                 if ( mitem[i][j] && stealth[i][j] )   /* stealth[x][y] = 1 when AWAKE! */
  112.                     {
  113.                     movelist[movecnt].x = i;
  114.                     movelist[movecnt].y = j ;
  115.                     if ( monster[mitem[i][j]].intelligence > min_int )
  116.                         {
  117.                         movelist[movecnt].smart = TRUE ;
  118.                         smart_count++;
  119.                         }
  120.                     else
  121.                         movelist[movecnt].smart = FALSE ;
  122.                     movecnt++;
  123.                     }
  124.         }
  125.  
  126.     /* now move the monsters in the movelist.  If we have at least one
  127.        smart monster, build a proximity ripple and use it for all smart
  128.        monster movement.
  129.     */
  130.     if (movecnt > 0 )
  131.         {
  132.         if ( c[SCAREMONST] )
  133.             for ( i = 0 ; i < movecnt ; i++ )
  134.                 move_scared( movelist[i].x, movelist[i].y );
  135.         else
  136.             {
  137.             if ( smart_count > 0 )
  138.                 {
  139.                 /* I was going to put in code that prevented the rebuilding
  140.                    of the proximity ripple if the player had not moved since
  141.                    the last turn.  Unfortunately, this permits the player to
  142.                    blast down doors to treasure rooms and not have a single
  143.                    intelligent monster move.
  144.                 */
  145.                 build_proximity_ripple();
  146.                 for ( i = 0 ; i < movecnt ; i++ )
  147.                     if ( movelist[i].smart )
  148.                         move_smart( movelist[i].x, movelist[i].y );
  149.                     else
  150.                         move_dumb( movelist[i].x, movelist[i].y );
  151.                 }
  152.             else
  153.                 for ( i = 0 ; i < movecnt ; i++ )
  154.                     move_dumb( movelist[i].x, movelist[i].y );
  155.             }
  156.         }
  157.  
  158.     /* Also check for the last monster hit.  This is necessary to prevent
  159.        the player from getting free hits on a monster with long range
  160.        spells or when stealthed.
  161.     */
  162.     if ( c[AGGRAVATE] || !c[STEALTH] )
  163.         {
  164.         /* If the last monster hit is within the move window, its already
  165.            been moved.
  166.         */
  167.     if ( ( ( lasthx < tmp3 || lasthx >= tmp4 ) ||
  168.            ( lasthy < tmp1 || lasthy >= tmp2 ) ) &&
  169.            mitem[lasthx][lasthy] )
  170.             {
  171.         if ( c[SCAREMONST] )
  172.                 move_scared( lasthx, lasthy );
  173.             else
  174.         if ( monster[mitem[lasthx][lasthy]].intelligence > min_int )
  175.                     {
  176.             if ( smart_count == 0 )
  177.                         build_proximity_ripple( );
  178.                     move_smart( lasthx, lasthy );
  179.                     }
  180.                 else
  181.                     move_dumb( lasthx, lasthy );
  182.             lasthx = w1x[0];   /* make sure the monster gets moved again */
  183.             lasthy = w1y[0];
  184.             }
  185.         }
  186.     else
  187.         {
  188.         /* If the last monster hit is within the move window, and not
  189.            asleep due to stealth, then it has already been moved.
  190.        Otherwise (monster outside window, asleep due to stealth),
  191.        move the monster and update the lasthit x,y position.
  192.         */
  193.     if ( ( lasthx < tmp3 || lasthx >= tmp4 ) ||
  194.              ( lasthy < tmp1 || lasthy >= tmp2 ) &&
  195.        mitem[lasthx][lasthy] || !stealth[lasthx][lasthy] )
  196.             {
  197.         if ( c[SCAREMONST] )
  198.                 move_scared( lasthx, lasthy );
  199.             else
  200.         if ( monster[mitem[lasthx][lasthy]].intelligence > min_int )
  201.                     {
  202.             if ( smart_count == 0 )
  203.                         build_proximity_ripple( );
  204.                     move_smart( lasthx, lasthy );
  205.                     }
  206.                 else
  207.                     move_dumb( lasthx, lasthy );
  208.             lasthx = w1x[0];   /* make sure the monster gets moved again */
  209.             lasthy = w1y[0];
  210.             }
  211.         }
  212.     }
  213.  
  214. static char screen[MAXX][MAXY];    /* proximity ripple storage */
  215.  
  216. /* queue for breadth-first 'search' build of proximity ripple.
  217. */
  218. #define MAX_QUEUE 100
  219. static struct queue_entry
  220.         {
  221.         char x ;
  222.         char y ;
  223.         char distance ;
  224.         } queue[MAX_QUEUE];
  225. static int queue_head = 0 ;
  226. static int queue_tail = 0 ;
  227.  
  228. /* put a location on the proximity ripple queue
  229. */
  230. #define PUTQUEUE( _x, _y, _d )          \
  231.     {                                   \
  232.     queue[queue_tail].x = (_x) ;        \
  233.     queue[queue_tail].y = (_y) ;        \
  234.     queue[queue_tail].distance = (_d);  \
  235.     queue_tail++;                       \
  236.     if (queue_tail == MAX_QUEUE)        \
  237.         queue_tail = 0 ;                \
  238.     }
  239.  
  240. /* take a location from the proximity ripple queue
  241. */
  242. #define GETQUEUE( _x, _y, _d )          \
  243.     {                                   \
  244.     (_x) = queue[queue_head].x ;        \
  245.     (_y) = queue[queue_head].y ;        \
  246.     (_d) = queue[queue_head].distance ; \
  247.     queue_head++;                       \
  248.     if (queue_head == MAX_QUEUE)        \
  249.         queue_head = 0 ;                \
  250.     }
  251.  
  252. /* check for the proximity ripple queue being empty
  253. */
  254. #define QUEUEEMPTY() (queue_head == queue_tail)
  255.  
  256. /*
  257.     For smart monster movement, build a proximity ripple from the player's
  258.     position, out to a 'distance' of 20.  For example:
  259.  
  260.     W 5 4 4 W W X    Player is at position marked 1
  261.     W 5 W 3 3 W W    W is a wall.  Monsters will attempt
  262.     W 6 W 2 W 4 W    to move to a location with a smaller
  263.     W 7 W 1 W 5 W    value than their current position.
  264.     W 8 W W W 6 W    Note that a monster at location X
  265.     W 9 9 8 7 7 7    will not move at all.
  266.     W W W 8 W W W
  267. */
  268. static void build_proximity_ripple()
  269.     {
  270.     int xl, yl, xh, yh ;
  271.     int k, m, z, tmpx, tmpy;
  272.     int curx, cury, curdist;
  273.  
  274.     xl=tmp3-2; yl=tmp1-2; xh=tmp4+2;  yh=tmp2+2;
  275.     vxy(&xl,&yl);  vxy(&xh,&yh);
  276.     for (k=yl; k<=yh; k++)
  277.     for (m=xl; m<=xh; m++)
  278.         {
  279.         switch(item[m][k])
  280.         {
  281.         case OWALL:
  282.         case OPIT:
  283.         case OTRAPARROW:
  284.         case ODARTRAP:
  285.         case OCLOSEDDOOR:
  286.         case OTRAPDOOR:
  287.         case OTELEPORTER:
  288.             screen[m][k]=127;
  289.             break;
  290.         case OENTRANCE:
  291.             if (level==1)
  292.                 screen[m][k] = 127;
  293.             else
  294.                 screen[m][k] = 0;
  295.             break;
  296.         default:
  297.             screen[m][k] = 0;
  298.             break;
  299.         };
  300.           }
  301.       screen[playerx][playery]=1;
  302.  
  303. /* now perform proximity ripple from playerx,playery to monster */
  304.       xl=tmp3-1; yl=tmp1-1; xh=tmp4+1;  yh=tmp2+1;
  305.       vxy(&xl,&yl);  vxy(&xh,&yh);
  306.  
  307.       PUTQUEUE( playerx, playery, 1 );
  308.       do
  309.       {
  310.       GETQUEUE( curx, cury, curdist );
  311.  
  312.       /* test all spots around the current one being looked at.
  313.       */
  314.       if ( ( curx >= xl && curx < xh ) &&
  315.            ( cury >= yl && cury < yh ) )
  316.           {
  317.           for (z=1; z<9; z++)
  318.           {
  319.           tmpx = curx + diroffx[z] ;
  320.           tmpy = cury + diroffy[z] ;
  321.           if (screen[tmpx][tmpy] == 0 )
  322.               {
  323.               screen[tmpx][tmpy] = curdist + 1;
  324.               PUTQUEUE( tmpx, tmpy, curdist + 1 );
  325.               }
  326.           }
  327.           }
  328.       }
  329.       while (!QUEUEEMPTY());
  330.  
  331.     }
  332.  
  333. /*
  334.     Move scared monsters randomly away from the player position.
  335. */
  336. static void move_scared( i, j )
  337. int i, j ;
  338.     {
  339.     int xl, yl, tmp, tmpitem ;
  340.  
  341.     /* check for a half-speed monster, and check if not to move.  Could be
  342.        done in the monster list build.
  343.     */
  344.     switch(mitem[i][j])
  345.         {
  346.         case TROGLODYTE:  case HOBGOBLIN:  case METAMORPH:  case XVART:
  347.         case INVISIBLESTALKER:  case ICELIZARD: if ((gtime & 1) == 1) return;
  348.         };
  349.  
  350.     if ((xl = i+rnd(3)-2) < 0)
  351.     xl=0;
  352.     if (xl >= MAXX)
  353.     xl=MAXX-1;
  354.     if ((yl = j+rnd(3)-2) < 0)
  355.     yl=0;
  356.     if (yl >= MAXY)
  357.     yl=MAXY-1;
  358.  
  359.     if ((tmp=item[xl][yl]) != OWALL)
  360.     if (mitem[xl][yl] == 0)
  361.         if ((mitem[i][j] != VAMPIRE) || (tmp != OMIRROR))
  362.         if (tmp != OCLOSEDDOOR)
  363.             mmove(i,j,xl,yl);
  364.     }
  365.  
  366. /*
  367.     Move monsters that are moving intelligently, using the proximity
  368.     ripple.  Attempt to move to a position in the proximity ripple
  369.     that is closer to the player.
  370.  
  371.     Parameters: the X,Y position of the monster to be moved.
  372. */
  373. static void move_smart( i, j )
  374. int i,j ;
  375.     {
  376.     int x,y,z ;
  377.  
  378.     /* check for a half-speed monster, and check if not to move.  Could be
  379.        done in the monster list build.
  380.     */
  381.     switch(mitem[i][j])
  382.         {
  383.         case TROGLODYTE:  case HOBGOBLIN:  case METAMORPH:  case XVART:
  384.         case INVISIBLESTALKER:  case ICELIZARD: if ((gtime & 1) == 1) return;
  385.         };
  386.  
  387.     /* find an adjoining location in the proximity ripple that is
  388.        closer to the player (has a lower value) than the monster's
  389.        current position.
  390.     */
  391.     if (mitem[i][j] != VAMPIRE)
  392.     for (z=1; z<9; z++) /* go around in a circle */
  393.         {
  394.         x = i + diroffx[z] ;
  395.         y = j + diroffy[z] ;
  396.         if ( screen[x][y] < screen[i][j] )
  397.         if ( !mitem[x][y] )
  398.             {
  399.             mmove(i,j,w1x[0]=x,w1y[0]=y);
  400.             return;
  401.             }
  402.         }
  403.     else
  404.     /* prevent vampires from moving onto mirrors
  405.     */
  406.     for (z=1; z<9; z++) /* go around in a circle */
  407.         {
  408.         x = i + diroffx[z] ;
  409.         y = j + diroffy[z] ;
  410.         if (( screen[x][y] < screen[i][j] ) &&
  411.         ( item[x][y] != OMIRROR ))
  412.         if ( !mitem[x][y] )
  413.             {
  414.             mmove(i,j,w1x[0]=x,w1y[0]=y);
  415.             return;
  416.             }
  417.         }
  418.  
  419.     }
  420.  
  421. /*
  422.    For monsters that are not moving in an intelligent fashion.  Move
  423.    in a direct fashion toward the player's current position.
  424.  
  425.    Parameters: the X,Y position of the monster to move.
  426. */
  427. static void move_dumb( i, j )
  428. int i, j ;
  429.     {
  430.     int xl, yl, xh, yh ;
  431.     int k, m, tmp, tmpd, tmpx, tmpy ;
  432.  
  433.     /* check for a half-speed monster, and check if not to move.  Could be
  434.        done in the monster list build.
  435.     */
  436.     switch(mitem[i][j])
  437.         {
  438.         case TROGLODYTE:  case HOBGOBLIN:  case METAMORPH:  case XVART:
  439.         case INVISIBLESTALKER:  case ICELIZARD: if ((gtime & 1) == 1) return;
  440.         };
  441.  
  442.     /* dumb monsters move here */
  443.     /* set up range of spots to check.  instead of checking all points
  444.        around the monster, only check those closest to the player.  For
  445.        example, if the player is up and right of the monster, check only
  446.        the three spots up and right of the monster.
  447.     */
  448.     xl=i-1;  yl=j-1;  xh=i+2;  yh=j+2;
  449.     if (i<playerx) xl++; else if (i>playerx) --xh;
  450.     if (j<playery) yl++; else if (j>playery) --yh;
  451.  
  452.     /* check all spots in the range.  find the one that is closest to
  453.        the player.  if the monster is already next to the player, exit
  454.        the check immediately.
  455.     */
  456.     tmpd = 10000 ;
  457.     tmpx = i ;  tmpy = j ;
  458.     for ( k = xl ; k < xh ; k++ )
  459.     for ( m = yl ; m < yh ; m++ )
  460.         if ( k == playerx && m == playery )
  461.         {
  462.         tmpd = 1 ;
  463.         tmpx = k ;
  464.         tmpy = m ;
  465.         break;       /* exitloop */
  466.         }
  467.         else if ((item[k][m] != OWALL) &&
  468.              (item[k][m] != OCLOSEDDOOR) &&
  469.              ((mitem[k][m] == 0 ) || (( k == i ) && ( m == j ))) &&
  470.              ((mitem[i][j] != VAMPIRE) || (item[k][m] != OMIRROR)))
  471.         {
  472.         tmp = (playerx-k)*(playerx-k)+(playery-m)*(playery-m);
  473.         if (tmp < tmpd)
  474.             {
  475.             tmpd = tmp;
  476.             tmpx = k;
  477.             tmpy = m;
  478.             }  /* end if */
  479.         }  /* end if */
  480.  
  481.     /* we have finished checking the spaces around the monster.  if
  482.        any can be moved on and are closer to the player than the
  483.        current location, move the monster.
  484.     */
  485.     if ((tmpd < 10000) && ((tmpx != i) || (tmpy != j)))
  486.     {
  487.     mmove( i, j, tmpx, tmpy );
  488.     w1x[0] = tmpx ;              /* for last monster hit */
  489.     w1y[0] = tmpy ;
  490.     }
  491.     else
  492.     {
  493.     w1x[0] = i ;              /* for last monster hit */
  494.     w1y[0] = j ;
  495.     }
  496.     }  /* end move_dumb() */
  497.  
  498. /*
  499.  *  mmove(x,y,xd,yd)    Function to actually perform the monster movement
  500.  *      int x,y,xd,yd;
  501.  *
  502.  *  Enter with the from coordinates in (x,y) and the destination coordinates
  503.  *  in (xd,yd).
  504.  */
  505. static void mmove(aa,bb,cc,dd)
  506.     int aa,bb,cc,dd;
  507.     {
  508.     register int tmp,i,flag;
  509.     char *who,*p;
  510.     flag=0; /* set to 1 if monster hit by arrow trap */
  511.     if ((cc==playerx) && (dd==playery))
  512.         {
  513.         hitplayer(aa,bb);
  514.         return;
  515.         }
  516.     i=item[cc][dd];
  517.     if ((i==OPIT) || (i==OTRAPDOOR))
  518.       switch(mitem[aa][bb])
  519.         {
  520.     case BAT:           case EYE:
  521.     case SPIRITNAGA:    case PLATINUMDRAGON:    case WRAITH:
  522.         case VAMPIRE:       case SILVERDRAGON:      case POLTERGEIST:
  523.         case DEMONLORD:     case DEMONLORD+1:       case DEMONLORD+2:
  524.         case DEMONLORD+3:   case DEMONLORD+4:       case DEMONLORD+5:
  525.         case DEMONLORD+6:   case DEMONPRINCE:   break;
  526.  
  527.         default:    mitem[aa][bb]=0; /* fell in a pit or trapdoor */
  528.         };
  529.     tmp = mitem[aa][bb];
  530.     mitem[cc][dd] = tmp;
  531.     if (i==OANNIHILATION)
  532.         {
  533.         if (tmp>=DEMONLORD+3) /* demons dispel spheres */
  534.             {
  535.             cursors();
  536.             lprintf("\nThe %s dispels the sphere!",monster[tmp].name);
  537.             rmsphere(cc,dd);    /* delete the sphere */
  538.             }
  539.         else mitem[cc][dd]=i=tmp=0;
  540.         }
  541.     stealth[cc][dd]=1;
  542.     if ((hitp[cc][dd] = hitp[aa][bb]) < 0) hitp[cc][dd]=1;
  543.     mitem[aa][bb] = 0;              
  544.     if (tmp == LEPRECHAUN)
  545.         switch(i)
  546.             {
  547.             case OGOLDPILE:  case OMAXGOLD:  case OKGOLD:  case ODGOLD:
  548.             case ODIAMOND:   case ORUBY:     case OEMERALD: case OSAPPHIRE:
  549.                     item[cc][dd] = 0; /* leprechaun takes gold */
  550.             };
  551.  
  552.     if (tmp == TROLL)  /* if a troll regenerate him */
  553.         if ((gtime & 1) == 0)
  554.             if (monster[tmp].hitpoints > hitp[cc][dd])  hitp[cc][dd]++;
  555.  
  556.     if (i==OTRAPARROW)  /* arrow hits monster */
  557.         { who = "An arrow";  if ((hitp[cc][dd] -= rnd(10)+level) <= 0)
  558.             { mitem[cc][dd]=0;  flag=2; } else flag=1; }
  559.     if (i==ODARTRAP)    /* dart hits monster */
  560.         { who = "A dart";  if ((hitp[cc][dd] -= rnd(6)) <= 0)
  561.             { mitem[cc][dd]=0;  flag=2; } else flag=1; }
  562.     if (i==OTELEPORTER) /* monster hits teleport trap */
  563.         { flag=3; fillmonst(mitem[cc][dd]);  mitem[cc][dd]=0; }
  564.     if (c[BLINDCOUNT]) return;  /* if blind don't show where monsters are   */
  565.     if (know[cc][dd] & HAVESEEN)
  566.         {
  567.         p=0;
  568.         if (flag) cursors();
  569.         switch(flag)
  570.           {
  571.           case 1: p="\n%s hits the %s";  break;
  572.           case 2: p="\n%s hits and kills the %s";  break;
  573.           case 3: p="\nThe %s%s gets teleported"; who="";  break;
  574.           };
  575.         if (p) { lprintf(p,who,monster[tmp].name); beep(); }
  576.         }
  577. /*  if (yrepcount>1) { know[aa][bb] &= 2;  know[cc][dd] &= 2; return; } */
  578.     if (know[aa][bb] & HAVESEEN)   show1cell(aa,bb);
  579.     if (know[cc][dd] & HAVESEEN)   show1cell(cc,dd);
  580.     }
  581.